package com.java1234.jxc.controller.admin;

import com.java1234.jxc.beans.SysLog;
import com.java1234.jxc.utils.StringUtil;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/admin/log")
public class LogAdminController {

    @Autowired
    private Dao dao;
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(true);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   //true:允许输入空值，false:不能为空值
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(@RequestParam(value = "type",defaultValue = "") String type,
                                    @RequestParam(value = "trueName",defaultValue = "") String trueName,
                                    @RequestParam(value = "btime",defaultValue = "") Date begin,
                                    @RequestParam(value = "etime",defaultValue = "") Date end,
                                    @RequestParam(value = "page",required = false,defaultValue = "1")Integer page,
                                    @RequestParam(value = "rows",required = false,defaultValue = "10")Integer rows) {
        Map<String, Object> result = new HashMap<>();

        StringBuffer sqlStr = new StringBuffer("select log.id,log.type,log.user_id,log.content,log.time" +
                " from t_log log  left JOIN t_user user ON user.id=log.user_id " +
                " where 1=1 ");
        StringBuffer countSql = new StringBuffer("select count(1) count" +
                " from t_log log  left JOIN t_user user ON user.id=log.user_id " +
                " where 1=1 ");

        if (StringUtil.isNotEmpty(type)){
            sqlStr.append(" and log.type = @type");
            countSql.append(" and log.type = @type");
        }
        if (StringUtil.isNotEmpty(trueName)){
            sqlStr.append(" and user.true_name like '%$trueName%'");
            countSql.append(" and user.true_name like '%$trueName%'");
        }

        if (begin!=null){
            sqlStr.append(" and log.time >= @begin");
            countSql.append(" and log.time >= @begin");
        }
        if (end != null){
            sqlStr.append(" and log.time <= @end");
            countSql.append(" and log.time <= @end");
        }

        Sql sql = Sqls.create(sqlStr.toString());
        sql.setPager(new Pager(page,rows));
        sql.setParam("type",type).setVar("trueName",trueName).setParam("begin",begin).setParam("end",end);
        //sql.setPager(new Pager(page,rows));

        sql.setCallback(new SqlCallback() {
            @Override
            public Object invoke(Connection connection, ResultSet rs, Sql sql) throws SQLException {
                List<SysLog> logs = new ArrayList<>();
                while (rs.next()){
                    SysLog log = new SysLog();
                    log.setId(rs.getInt("id"));
                    log.setContent(rs.getString("content"));
                    log.setTime(rs.getDate("time"));
                    log.setType(rs.getString("type"));
                    log.setUserId(rs.getInt("user_id"));
                    logs.add(log);
                }



                return logs;
            }
        });
        dao.execute(sql);
        List<SysLog> list = sql.getList(SysLog.class);
        list.forEach(item->{
            dao.fetchLinks(item,null);
        });

        result.put("rows",list);
        result.put("total",dao.count(SysLog.class,null));
        return result;
    }


}
